/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.terminal;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.common.CommonUtils;
import cz.vutbr.fit.VYPe.team18.VYPe11.exception.SematicException;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.data_type.DataType;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.literal.Literal;

public class LiteralExpressionNode extends TerminalExpression {
	private Literal literal;
	
	public LiteralExpressionNode(Literal literal) {
		this.literal=literal;
	}

	public Literal getLiteral() {
		return literal;
	}
	
	@Override
	public DataType getType() throws SematicException {
		return this.literal.getType();
	}

	@Override
	public void generateTAC() throws IOException {
		DataType dt=literal.getType();
		String strLiteral;
		if(dt==DataType.INT || dt==DataType.SHORT) {
			strLiteral=this.literal.getValue().toString();
		}
		else if(dt==DataType.CHAR) {
			strLiteral="'"+CommonUtils.escapeWhitespace(literal.getValue().toString())+"'";
		}
		else { // string
			strLiteral="\""+CommonUtils.escapeWhitespace((String)literal.getValue())+"\"";
		}
		this.writeTAC("=", strLiteral, "", "_t"+ tacCounter.getNextTermIndex());
	}
}
